package com.boilerCloud.controller.device;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.DeviceInfo;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.sys.User;
import com.boilerCloud.service.device.IDeviceService;
import com.boilerCloud.service.log.ILogService;


@RestController
@RequestMapping("/device/")
public class DeviceControler {
	private static final Logger log = LoggerFactory.getLogger(DeviceControler.class);
	@Resource
	private IDeviceService deviceService;



	@RequestMapping(value = "deviceList")
	public ModelAndView accountList(HttpServletRequest request) {
		User user = (User)request.getSession().getAttribute("usersession");
		if(user.getRoleId() == 2 || user.getRoleId() == 3){//app 用户
			return new ModelAndView("/views/device/deviceInfo_app.html");
		}
		return new ModelAndView("/views/device/deviceInfo.html");
	}
	
	@RequestMapping(value = "deviceLocation")
	public ModelAndView deviceLocation() {
		return new ModelAndView("/views/device/deviceLocation.html");
	}
	
	
	@RequestMapping(value = "deviceTemplate")
	public ModelAndView deviceTemplate() {
		return new ModelAndView("/views/device/deviceTemplate.html");
	}

	
	
	@RequestMapping(value = "deviceMonitor")
	public ModelAndView deviceMonitor() {
		return new ModelAndView("/views/device/deviceMonitor.html");
	}

	/**
	 * 查询设备列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryDeviceList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryDeviceList(@ModelAttribute DeviceInfo device ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			User user = (User)request.getSession().getAttribute("usersession");
			if(user.getRoleId() == 2){//app 用户
				device.setUserId(user.getId());
			}
			if(user.getRoleId() == 3){//经销商
				device.setDealerId(user.getId());
			}
			HzlqswPage<DeviceInfo> page = new HzlqswPage<DeviceInfo>();
			page.setCurPage(yema);
			page.setPageSize(length);
			deviceService.qryList(device,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询设备列表失败");
			e.printStackTrace();
			log.error("查询设备列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 修改设备信息
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("修改设备")
	@RequestMapping(value = "editDevice", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editDevice(
			@ModelAttribute DeviceInfo device ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			deviceService.edit(device);
			rst.setResultCode(ReqResult.resultCode_Success);
			
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改设备信息失败");
			log.error("修改设备信息异常",e);
			return rst;
		}
	}
	/**
	 * 设备绑定项目
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "bindDeviceProject", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult bindDeviceProject(
			@ModelAttribute DeviceInfo device ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			log.info(device.toString());
			deviceService.bindDeviceProject(device);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;
		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改设备信息失败");
			log.error("修改设备信息异常",e);
			return rst;
		}
	}
	/**
	 * 解除设备绑定项目
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "celBindDeviceProject", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult celBindDeviceProject(
			@ModelAttribute DeviceInfo device ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			log.info(device.toString());
			device.setProjectId(-1);
			deviceService.celbindDeviceProject(device);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;
		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改设备信息失败");
			log.error("修改设备信息异常",e);
			return rst;
		}
	}
	/**
	 * 新增设备
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("新增设备")
	@RequestMapping(value = "addDevice", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addDevice(
			@ModelAttribute DeviceInfo device ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			deviceService.addDevice(device);
			rst.setResultCode(ReqResult.resultCode_Success);
		
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增设备信息失败, 请检查设备信息是否填写正确");
			log.error("新增设备信息异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除设备
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delDevice", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delDevice(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			deviceService.delDevice(id);
			
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除设备失败");
			log.error("删除设备异常",e);
			return rst;
		}
	}
	
	/**
	 * 获取设备信息
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getDeviceInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getDeviceInfo(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			DeviceInfo device  = deviceService.getDeviceInfo(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(device);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除设备失败");
			log.error("删除设备异常",e);
			return rst;
		}
	}
	


	@RequestMapping(value = "getDeviceList",  produces = "text/plain;charset=UTF-8")
	public ReqResult getDeviceList(
			int type ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			DeviceInfo device = new DeviceInfo();
			User user = (User)request.getSession().getAttribute("usersession");
			if(user.getRoleId() == 2){//app 用户
				device.setUserId(user.getId());
			}
			if(user.getRoleId() == 3){//经销商
				device.setDealerId(user.getId());
			}
			Object o = deviceService.getDeviceList(type, device);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(o);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增设备信息失败");
			log.error("新增设备信息异常",e);
			return rst;
		}
	}

	/**
	 * 获取设备历史报警记录
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getAlarmRecordList",  produces = "text/plain;charset=UTF-8")
	public ReqResult getAlarmRecordList(
			int id ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			Object o = deviceService.getAlarmRecordList(id,rst);
			if(o !=null){
				
				rst.setResultCode(ReqResult.resultCode_Success);
				rst.setReturnObject(o);
			}
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取设备报警记录失败");
			log.error("获取设备报警记录失败异常",e);
			return rst;
		}
	}

	/**
	 * 获取设备当前报警记录
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getCurrAlarmRecordList",  produces = "text/plain;charset=UTF-8")
	public ReqResult getCurrAlarmRecordList(
			int id ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			Object o = deviceService.getCurrAlarmRecordList(id,rst);
			if(o !=null){
				
				rst.setResultCode(ReqResult.resultCode_Success);
				rst.setReturnObject(o);
			}
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取设备报警记录失败");
			log.error("获取设备报警记录失败异常",e);
			return rst;
			
		}
	}
	
	@RequestMapping(value = "getCurrentDataList",  produces = "text/plain;charset=UTF-8")
	public ReqResult getCurrentDataList(
			int id ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			Object o = deviceService.getCurrentDataList(id,rst);
			if(o !=null){
				
				rst.setResultCode(ReqResult.resultCode_Success);
				rst.setReturnObject(o);
			}
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取设备数据记录失败");
			log.error("获取设备数据记录异常",e);
			return rst;
		}
	}
	
	
	@RequestMapping(value = "getHistoryDataList",  produces = "text/plain;charset=UTF-8")
	public ReqResult getHistoryDataList(
			int id ,long channel,String begintime,String endtime,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			Object o = deviceService.getHistoryDataList(id,channel,begintime,endtime,rst);
			if(o !=null){
				
				rst.setResultCode(ReqResult.resultCode_Success);
				rst.setReturnObject(o);
			}
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取设备数据记录失败");
			log.error("获取设备数据记录异常",e);
			return rst;
		}
	}
	
	@RequestMapping(value = "qryChannel",  produces = "text/plain;charset=UTF-8")
	public ReqResult qryChannel(
			int id ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			Object o = deviceService.qryChannel(id,rst);
			if(o !=null){
				
				rst.setResultCode(ReqResult.resultCode_Success);
				rst.setReturnObject(o);
			}
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取设备数据记录失败");
			log.error("获取设备数据记录异常",e);
			return rst;
		}
	}

}
